package com.deimos.tests;

import static org.junit.Assert.assertTrue;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.vast.ows.sps.SPSUtils;
import org.vast.ows.sps.StatusReport;
import org.vast.ows.sps.StatusReport.RequestStatus;

import com.deimos.HMA;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.TaskController;
import com.deimos.dataBase.utils.SetScenarioServlet;

public class Scenarios {
    private static HMA hma = new HMA();
    SPSUtils utils = new SPSUtils();
    static String taskId = null;
    private String DATE_FORMAT_HMA = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    private SimpleDateFormat sdf_hma = new SimpleDateFormat(DATE_FORMAT_HMA);

    @BeforeClass
    public static void init() {
	TaskController taskController = new TaskController();
	taskController.createTables(false);
    }
    
    @Test
    public void available() throws Exception {
	SetScenarioServlet setScenario = new SetScenarioServlet();
	SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	String requestHttp = "Scenario=Scenario-TC-0000&StartTime=" + DATEFORMAT.format(new Date());
	setScenario.setScenario(requestHttp, "http://localhost:8086/HMA-SPS/SetScenarioServlet");

	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Scenario_Submit_Request.xml").replace("MINTIME", sdf_hma.format(new Date())).replace("MAXTIME", sdf_hma.format(new Date(new Date().getTime() + ((long) 360 * 24 * 60 * 60 * 1000)))));
	response = hma.Submit(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    bad_weather();
	}
	

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    report = UtilsDeimos.readParams(response.toString()).getReport();
	    assertTrue(report.getRequestStatus() == RequestStatus.Accepted);
	}
    }

    @Test
    public void bad_weather() throws Exception {
	SetScenarioServlet setScenario = new SetScenarioServlet();
	SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	String requestHttp = "Scenario=Scenario-TC-0030&StartTime=" + DATEFORMAT.format(new Date());
	setScenario.setScenario(requestHttp, "http://localhost:8086/HMA-SPS/SetScenarioServlet");

	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Scenario_Submit_Request.xml").replace("MINTIME", sdf_hma.format(new Date())).replace("MAXTIME", sdf_hma.format(new Date(new Date().getTime() + ((long) 360 * 24 * 60 * 60 * 1000)))));
	response = hma.Submit(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    bad_weather();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    report = UtilsDeimos.readParams(response.toString()).getReport();
	    assertTrue(report.getRequestStatus() == RequestStatus.Rejected && report.getDescription().contains("is over the max limit"));
	}
    }
    
    @Test
    public void sensor_unavailable() throws Exception {
	
	SetScenarioServlet setScenario = new SetScenarioServlet();
	SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	String requestHttp = "Scenario=Scenario-TC-0010&StartTime=" + DATEFORMAT.format(new Date());
	setScenario.setScenario(requestHttp, "http://localhost:8086/HMA-SPS/SetScenarioServlet");

	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Scenario_Submit_Request.xml").replace("MINTIME", sdf_hma.format(new Date())).replace("MAXTIME", sdf_hma.format(new Date(new Date().getTime() + ((long) 360 * 24 * 60 * 60 * 1000)))));
	response = hma.Submit(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    sensor_unavailable();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    report = UtilsDeimos.readParams(response.toString()).getReport();
	    assertTrue(report.getRequestStatus() == RequestStatus.Rejected && report.getDescription().contains("Sensor OPT-2 unavailable for segment"));
	}
    }
    
    
    @Test
    public void stations_unavailable() throws Exception {
	
	SetScenarioServlet setScenario = new SetScenarioServlet();
	SimpleDateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	String requestHttp = "Scenario=Scenario-TC-0020&StartTime=" + DATEFORMAT.format(new Date());
	setScenario.setScenario(requestHttp, "http://localhost:8086/HMA-SPS/SetScenarioServlet");

	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Scenario_Submit_Request.xml").replace("MINTIME", sdf_hma.format(new Date())).replace("MAXTIME", sdf_hma.format(new Date(new Date().getTime() + ((long) 360 * 24 * 60 * 60 * 1000)))));
	response = hma.Submit(request);

	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	if (report.getDescription().contains("in use")) {
	    if (report.getDescription().contains("task:"))
		taskId = report.getDescription().split("task:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    if (report.getDescription().contains("tasks:"))
		taskId = report.getDescription().split("tasks:")[1].split(",")[0].replace(" ", "").replace("Z.", "Z");
	    cancel_is_in_use();
	    taskId = null;
	    stations_unavailable();
	}

	if (taskId == null) {
	    taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	    report = UtilsDeimos.readParams(response.toString()).getReport();
	    assertTrue(report.getRequestStatus() == RequestStatus.Rejected && report.getDescription().contains("Stations not available for segment"));
	}
    }

    
    
    @After
    public void cancel_is_in_use() throws Exception {
	OMElement request;
	if (taskId == null)
	    return;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "Scenario_Cancel_Request.xml").replace("TASKID", taskId));
	hma.Cancel(request);
	taskId = null;
    }
}
